package mystack;

import java.util.Arrays;

public class MyStack {

    private int[] elem;
    private int usedSize;

    private static final int DEFAULT_CAPACITY = 10;

    public MyStack() {
        this.elem = new int[DEFAULT_CAPACITY];
    }

    public void push(int val) {
        if(this.usedSize == elem.length) {
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[this.usedSize] = val;
        this.usedSize++;

    }

    private boolean isEmpty() {
        if (this.usedSize == 0) {
            return true;
        }else {
            return false;
        }
    }

    public int peek() {
        if(isEmpty()) {
            throw new RuntimeException("栈为空");
        }
        return this.elem[usedSize-1];
    }

    public int pop() {
        int e = peek();
        this.usedSize--;
        return e;
    }


}
